/*
 * 
 * Copyright ? 2006 Shanghai je01 Co. Ltd.
 * All right reserved
 * 
 * @author Ray
 * Created on 2006-4-24
 */

package com.jycrm.presentation.core;

import java.io.OutputStream;
import java.util.EventListener;
import java.util.Locale;

import javax.swing.event.EventListenerList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.AbstractPage;
import org.apache.tapestry.ApplicationRuntimeException;
import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.IComponent;
import org.apache.tapestry.IEngine;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.event.ChangeObserver;
import org.apache.tapestry.event.PageDetachListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.event.PageRenderListener;
import org.apache.tapestry.event.PageValidateListener;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.html.HTMLWriter;
import org.apache.tapestry.util.StringSplitter;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jycrm.service.core.impl.SpringService;

public abstract class JyBaseComponent extends BaseComponent implements PageDetachListener,IFormComponent{
    
    private static final Log LOG = LogFactory.getLog(AbstractPage.class);

    /**
     *  The {@link IEngine} the page is currently attached to.
     *
     **/
    private IEngine _engine;

    /**
     *  The visit object, if any, for the application.  Set inside
     *  {@link #attach(IEngine)} and cleared
     *  by {@link #detach()}.
     *
     **/
    private Object _visit;

    /**
     *  The qualified name of the page, which may be prefixed by the
     *  namespace.
     * 
     *  @since 2.3
     * 
     **/

    private String _pageName;

    /**
     *  Set when the page is attached to the engine.
     *
     **/

    private IRequestCycle _requestCycle;

    /**
     *  The locale of the page, initially determined from the {@link IEngine engine}.
     *
     **/

    public JyBaseComponent()
    {
        initialize();
    }

    /**
     *  Prepares the page to be returned to the pool.
     *  <ul>
     *  <li>Clears the changeObserved property
     *  <li>Invokes {@link PageDetachListener#pageDetached(PageEvent)} on all listeners
     *  <li>Invokes {@link #initialize()} to clear/reset any properties 
     * <li>Clears the engine, visit and requestCycle properties
     *  </ul>
     *
     *  <p>Subclasses may override this method, but must invoke this
     *  implementation (usually, last).
     *
     **/

    public void detach(){
        Tapestry.addMethodInvocation(Tapestry.ABSTRACTPAGE_DETACH_METHOD_ID);
        // Do this first,so that any changes to persistent properties do not
        // cause errors.
        initialize();
        _engine = null;
        _visit = null;
        _requestCycle = null;
    }

    /**
     *  Method invoked from the constructor, and from
     *  {@link #detach()} to (re-)initialize properties
     *  of the page.  This is most useful when
     *  properties have non-null initial values.
     * 
     *  <p>Subclasses may override this implementation
     *  (which is empty).
     * 
     *  @since 2.2
     * 
     **/

    protected void initialize(){
        // Does nothing.
    }

    public IEngine getEngine(){
        return _engine;
    }

    /**
     *  Called by the {@link IEngine engine} to attach the page
     *  to itself.  Does
     *  <em>not</em> change the locale, but since a page is selected
     *  from the {@link org.apache.tapestry.engine.IPageSource} pool based on its
     *  locale matching the engine's locale, they should match
     *  anyway.
     *
     **/

    public void attach(IEngine value)
    {
        if (_engine != null)
            LOG.error(this +" attach(" + value + "), but engine = " + _engine);

        _engine = value;
    }
    
    public Object getGlobal(){
        return getPage().getEngine().getGlobal();       
    }

    

    
  

   
   
}
